
import {
  login
} from '@/api/user'
import { mapActions, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback('账号不能为空')
      } else {
        callback()
      }
    }
    // const validatePassword = (rule, value, callback) => {
    //   let validationStr = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/
    //   if (value === '') {
    //     callback(new Error('请输入密码'));
    //   } else if (!validationStr.test(value)) {
    //     callback(new Error('长度至少6位，需要包涵数字和字母'));
    //   } else {
    //     callback();
    //   }
    // },
    // data() {
    //   const validateUsername = (rule, value, callback) => {
    //     // if (!validUsername(value)) {
    //     //   callback(new Error("请输入正确的用户名"));
    //     // } else {
    //     //   callback();
    //     // }
    //     callback()
    //   }
      const validatePassword = (rule, value, callback) => {
        if (value.length < 6) {
          callback(new Error('密码不少于6位字符'))
        } else {
          callback()
        }
      }
    return {
      loginForm: {
        username: '',
        password: '',
        loginRoleType: 0,
        checked: false
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  mounted() {
  },
  methods: {
    ...mapMutations('user', ['SET_PASSWORD', 'SET_USERNAME']),
    ...mapActions('socket', ['startWebSocket']),
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          login(this.loginForm).then(res => {
            if (res.data.code == 200) {
              let username = res.data.data.sysUser.username
              let password = res.data.data.sysUser.password
              this.SET_USERNAME(username)
              this.SET_PASSWORD(password)
              localStorage.setItem('username', username)
              this.$router.push('sample-management/decision')
            } else {
              this.loading = false
              this.$message.error(res.data.message)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },


  },
}
